package me.luger.project.fof.service;

import me.luger.core.commom.util.DateUtil;
import me.luger.core.dao.nutz.service.BaseService;
import me.luger.project.fof.config.cache.redis.RedisManager;
import me.luger.project.fof.entity.FundCollect;
import org.nutz.dao.Dao;
import org.nutz.dao.Sqls;
import org.nutz.dao.sql.Sql;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository
public class FundCollectService extends BaseService<FundCollect> {
    @Autowired
    private RedisManager redisManager;

    public FundCollectService(Dao dao) {
        super(dao);
    }

    public boolean collect(Integer uid,String fundExtCode){
        try{
            FundCollect collect = new FundCollect();
            collect.setUid(uid);
            collect.setFundExtCode(fundExtCode);
            collect = this.insert(collect);
            Double now = DateUtil.nowAsDate().getTime() + 0D;
            redisManager.zadd("fof:user:fund:collect:"+uid,now,String.valueOf(fundExtCode));
            return true;
        }catch (Exception e){
            return false;
        }

    }

    public boolean unCollect(Integer uid,String fundExtCode){
        Sql delSql = Sqls.create("select * from fund_collect where uid = @uid and fund_ext_code = @fundExtCode");
        delSql.setParam("uid",uid);
        delSql.setParam("fundExtCode",fundExtCode);
        FundCollect collect = this.fetchOne(delSql);
        if(null == collect){
            return false;
        }
        this.delete(new Integer[]{collect.getId()});
        redisManager.zrem("fof:user:fund:collect:"+uid,String.valueOf(fundExtCode));
        return true;
    }

    public boolean isCollect(Integer uid, String fundExtCode) {
        Double collectTime = redisManager.zscore("fof:user:fund:collect:"+uid,String.valueOf(fundExtCode));
        if(null != collectTime && collectTime > 0){
            return true;
        }
        return false;
    }




}
